/**  
 * @Title: FlowService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-03-23 15:09:58 
 */  
package com.fsxgt.datagrid.flow.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fsxgt.datagrid.flow.bo.FlowComponentBO;
import com.fsxgt.datagrid.flow.bo.FlowInfoBO;
import com.fsxgt.datagrid.flow.bo.FlowRelationBO;
import com.fsxgt.datagrid.flow.dao.FlowInfoMapper;
import com.fsxgt.datagrid.flow.exception.FlowException;


/**  
 * @ClassName: FlowService
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-03-23 15:09:58 
*/
@Service
public class FlowService {

	@Autowired
	private FlowInfoMapper flowInfoMapper;
	
	@Autowired
	private CmpRegistryService cmpRegistryService;

	@Transactional(rollbackFor = Exception.class)
	public void saveFlow(FlowInfoBO flowInfoBO) throws FlowException {
		
		
		
		for(FlowComponentBO flowComponentBO:flowInfoBO.getComponents()) {
			flowComponentBO.setId(UUID.randomUUID().toString());
			flowComponentBO.setFlowInfoId(flowInfoBO.getId());
			if(flowComponentBO.getEnable()==null) {
				flowComponentBO.setEnable("true");
			}
		}
		
		
		if(flowInfoBO.getComponents().size()>0) {
			//获取前端已经删除的组件
			List<FlowComponentBO> list = flowInfoMapper.findFlowComponentsIsDelete(flowInfoBO.getId(), flowInfoBO.getComponents());
			for(FlowComponentBO bo:list) {
				cmpRegistryService.getService(bo.getComponentType()).deleteComponent(flowInfoBO.getId(), bo.getComponentSeq());
			}
		}

		flowInfoBO.setUpdateTime(new Date());
		
		flowInfoMapper.updateFlowInfo(flowInfoBO);

		
		
		
		flowInfoMapper.deleteFlowComponentsByFlowId(flowInfoBO.getId());
		if(flowInfoBO.getComponents().size()>0) {
			flowInfoMapper.insertFlowComponents(flowInfoBO.getComponents());
		}
		flowInfoMapper.deleteFlowRelationsByFlowId(flowInfoBO.getId());
		
		for(FlowRelationBO relationBO:flowInfoBO.getRelations()) {
			relationBO.setFlowInfoId(flowInfoBO.getId());
			relationBO.setId(UUID.randomUUID().toString());
		}
		if(flowInfoBO.getRelations().size()>0) {
			flowInfoMapper.insertFlowRelations(flowInfoBO.getRelations());
		}
		
	}
	
	
	public FlowInfoBO getFlow(String flowId) {
		
		
		
		FlowInfoBO flowInfo = flowInfoMapper.findFlowInfoById(flowId);
		flowInfo.setComponents(flowInfoMapper.findFlowComponentsByFlowId(flowId));
		flowInfo.setRelations(flowInfoMapper.findFlowRelationsByFlowId(flowId));
		
		return flowInfo;
		
	}


	
	public  FlowInfoBO getFlowAndConfig(String flowId) throws FlowException {
		
		
		
		FlowInfoBO flowInfoBO=getFlow(flowId);
		
		flowInfoBO.setComponents(flowInfoBO.getComponents().stream().filter(bo-> bo.getEnable()!=null&&bo.getEnable().equals("true")).collect(Collectors.toList()));
		
		Map<String, FlowComponentBO> componentMap = flowInfoBO.getComponents().stream()
				.collect(Collectors.toMap(bo -> (bo.getComponentSeq()), bo -> (bo)));
		
		flowInfoBO.setRelations(flowInfoBO.getRelations().stream().filter(bo-> componentMap.containsKey(bo.getCmpFrom())&&componentMap.containsKey(bo.getCmpTo())).collect(Collectors.toList()));
		
		for(FlowComponentBO flowComponentBO:flowInfoBO.getComponents()) {
			flowComponentBO.setConfig(cmpRegistryService.getService(flowComponentBO.getComponentType()).getConfig(flowInfoBO.getId(), flowComponentBO.getComponentSeq()));
		}
		return flowInfoBO;
	}
	

}
